﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：审计模型
 */
var refreshGirdData; // 更新数据
var selectedRow;

var flag = false;
var planId = "";
var bootstrap = function ($, learun) {
    "use strict";

    var modelId = '';

    var page = {
        init: function () {
            page.bind();
            page.initGrid(false, 20);
        },
        bind: function () {
            // 计划
            $('#planId').lrselect({
                type: 'tree',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        planId = item.value;
                        page.search();
                    }
                }
            });
            $('#planId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'auditmodelwarnplans/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSetFirst();
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            $('#dealFlag_select').lrselect({placeholder: "是否整改"});
            // 公司选择
            $('#companyId').lrselect({
                type: 'tree',
                placeholder: '==请选择单位==',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true
            });
            $('#companyId').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree_all?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            });

            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });

            // 模型
            $('#model_select').lrselect({
                type: 'default',
                placeholder: '==请选择指标模型==',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        modelId = item.value;
                    } else {
                        modelId = '';
                    }
                   var keyword = $("#txt_Keyword").val();
                }
            });
            $('#model_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'auditmodels/dropdownList?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 开启多选
            $('#lr_multiselect').on('click', function () {
                flag = !flag;
                $('#gridtable').prop('outerHTML', '<div class="lr-layout-body" id="gridtable"></div>');
                setTimeout(function () {
                    page.initGrid(flag, 20);
                    page.search(params);
                }, 200);
            });
            // 查看
            $('#lr_view').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var modelId = $('#gridtable').jfGridValue('modelId');
                var keyValue = $('#gridtable').jfGridValue('id');
                if( !!keyValue && keyValue.toString().split(",").length > 1) {
                    learun.alert.warning('请选择单条记录查看详情！');
                    return false;
                }
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                       id: 'Form',
                       title: '查看告警',
                       url: config.webportal_server + 'pages/finance/audit_rule/preview_result/result' + modelId + '.html?modelId=' + modelId + '&warnId=' + keyValue + '&access_token=' + config.getToken().access_token,
                       width: 1000,
                       height: 800,
                       maxmin: true,
                       btn: null,
                       callBack: function (id) {
                           return top[id].acceptClick(refreshGirdData);
                       }
                   });
                }
            });
            //下发
            $('#lr_send').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认下发选中项?', function (res) {
                        if (res) {
                            learun.loading(true, '操作进行中...');
                            top.learun.loading(true, '操作进行中...');
                            learun.postForm(config.base_server() + 'auditmodelwarns/send?ids=' + keyValue + '&status=1' + '&access_token=' + config.getToken().access_token, {}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            //整改
            $('#lr_deal').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('modelId');
                var warnId = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'warn_deal' + keyValue,
                        title: '告警整改',
                        url: config.webportal_server + '/pages/workflow/wfmyTask/custmerWorkFlowForm.html?keyValue=' + keyValue + '&modelId=' + keyValue + '&warnId=' + warnId + '&shcemeCode=warn_deal&tabIframeId=5456a5f791844dbaaa1474af2a70e8db&type=0',
                        width: 1000,
                        height: 800,
                        maxmin: true,
                        btn: null,
                        end: function () {
                            refreshGirdData();
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'auditmodelwarns/delete?ids=' + keyValue + '&access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                var companyId = $("#companyId").lrselectGet();
                var planId = $("#planId").lrselectGet();
                var modelId = $("#model_select").lrselectGet();
                var dealFlag = $("#dealFlag_select").lrselectGet();

                var keyValue = $('#gridtable').jfGridValue('id');
                if(!flag) {
                    keyValue = '';
                }

//                if ((!companyId||companyId == '-1')&&(!modelId||modelId == '-1')) {
//                    learun.alert.warning('请先选择单位或指标模型！');
//                    return false;
//                }
                location.href = config.base_server() + 'auditmodelwarns/exportPdf?companyId=' + companyId + '&planId=' + planId + '&modelId=' + modelId + '&dealFlag=' + dealFlag + '&keyValue=' + keyValue + '&access_token=' + config.getToken().access_token;
            });
        },
        initGrid: function (multi, rowsSize) {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'auditmodelwarns/list_all?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "单位", name: "companyName", width: 200, align: "left"},
                    {label: "模型名称", name: "modelName", width: 280, align: "left"},
                    {
                        label: "告警级别", name: "warnLevel", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            if (cellvalue == 2) {
                                return "<span style='color: red'>红色预警</span>";
                            } else {
                                return "<span style='color: goldenrod'>黄色预警</span>";
                            }
                        }
                    },
                    {
                        label: '是否整改', name: 'dealFlag', width: 60, align: 'center',
                        formatter: function (cellvalue) {
                            if (cellvalue == 1) {
                                return "<span style='color: blue'>整改中</span>";
                            } else if (cellvalue == 2) {
                                return "<span style='color: green'>已整改</span>";
                            } else {
                                return "";
                            }
                        }
                    },
                    {label: "整改周期", name: "dealEnddate", width: 120, align: "center"},
                    {
                        label: "确认有效", name: "operate", width: 190, align: "center", sort: true,
                        formatter: function (cellvalue, row, op, $cell) {
                            $cell.on('click', '.confirm', function () {
                                 if ($cell.html().trim() == '') {
                                     return false;
                                 }
                                 var keyvalue = row.id;
                                 if (learun.checkrow(keyvalue)) {
                                     learun.layerConfirm('是否确认下发?', function (res) {
                                         if (res) {
                                             if (learun.checkrow(keyvalue)) {
                                                 learun.loading(true, '操作进行中...');
                                                 top.learun.loading(true, '操作进行中...');
                                                 learun.postForm(config.base_server() + 'auditmodelwarns/send?ids=' + keyvalue + '&status=1' + '&access_token=' + config.getToken().access_token, {}, function () {
                                                     refreshGirdData();
                                                 });
                                             }
                                         }
                                     });
                                 }
                                 return false;
                            });
                            $cell.on('click', '.cancelConfirm', function () {
                                if ($cell.html().trim() == '') {
                                    return false;
                                }
                                var keyvalue = row.id;
                                if (learun.checkrow(keyvalue)) {
                                    learun.layerConfirm('是否确认取消下发?', function (res) {
                                        if (res) {
                                            if (learun.checkrow(keyvalue)) {
                                                learun.loading(true, '操作进行中...');
                                                top.learun.loading(true, '操作进行中...');
                                                learun.postForm(config.base_server() + 'auditmodelwarns/send?ids=' + keyvalue + '&status=2' + '&access_token=' + config.getToken().access_token, {}, function () {
                                                    refreshGirdData();
                                                });
                                            }
                                        }
                                    });
                                }
                                return false;
                            });
                            var html = '';
                            if (row.status == '1') {
                                html += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="label cancelConfirm" style="cursor: pointer;background-color:grey;">取消</span>';
                            } else {
                                html += '<span class="label label-info confirm" style="cursor: pointer;">下发</span>';
                            }
                            return html;
                        }
                    },
                    {label: "备注", name: "remark", width: 500, align: "left"},
                    {label: "modelId", name: "modelId", width: 1, align: "left", hidden: true},
                    {label: "id", name: "id", width: 1, align: "left", hidden: true}
                ],
                dblclick: function (row) {
                    selectedRow = $('#gridtable').jfGridGet('rowdata');
                    if (learun.checkrow(row)) {
                        learun.layerForm({
                            id: 'Form',
                            title: '查看告警',
                            url: config.webportal_server + 'pages/finance/audit_rule/preview_result/result' + row.modelId + '.html?modelId=' + row.modelId + '&warnId=' + row.id + '&access_token=' + config.getToken().access_token,
                            width: 1000,
                            height: 800,
                            maxmin: true,
                            btn: null,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                rows: rowsSize,
                isMultiselect: multi,
                reloadSelected: true,
                mainId: 'id',
            });
        },
        search: function (param) {
            param = param || {};
            param.modelId = modelId;

            var companyId = $("#companyId").lrselectGet();
//            var planId = $("#planId").lrselectGet();
            var deal = $("#dealFlag_select").lrselectGet();

            param.companyId = companyId;
            param.planId = planId;
            param.dealFlag = deal;

            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


